package exampleQuestions;

import java.util.Stack;

public class ReverseStack {

	/**
	 * reverse a stack in place using recursion
	 */
	public static void main(String[] args) {

		Stack<Integer> stack = new Stack<Integer>();

		stack.push(0);
		stack.push(1);
		stack.push(2);
		stack.push(3);
		
		reverseStack(stack);

		while (!stack.isEmpty()) {
			System.out.println(stack.pop());
		}

	}

	private static void reverseStack(Stack<Integer> stack) {
		if(!stack.isEmpty()) {
			int val = stack.pop();
			reverseStack(stack);
			push(val, stack);
		}
	}

	private static void push(int val, Stack<Integer> stack) {
		if(!stack.empty()) {
			int s = stack.pop();
			push(val, stack);
			stack.push(s);
		} else {
			stack.push(val);
		}
	}
}
